/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package org.apache.coyote.http11;

import javax.management.ObjectName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.modeler.Registry;
import org.apache.coyote.RequestInfo;
import org.apache.tomcat.util.net.PoolTcpEndpoint;
import org.apache.tomcat.util.threads.ThreadPool;

public class LegacyAccessibleHttp11Protocol extends Http11Protocol {

    public static final int THREAD_DATA_PROCESSOR = Http11BaseProtocol.THREAD_DATA_PROCESSOR;

    @Override
    protected Http11ConnectionHandler createConnectionHandler() {
        LegacyAccessibleHttp11ConnectionHandler handler = createAccessibleConnectionHandler();
        if (handler != null) {
            return handler;
        } else {
            return super.createConnectionHandler();
        }
    }
    
    protected LegacyAccessibleHttp11ConnectionHandler createAccessibleConnectionHandler() {
        return null;
    }
    
    public static class LegacyAccessibleHttp11ConnectionHandler extends Http11ConnectionHandler {
        
        private static final Log log = LogFactory.getLog(LegacyAccessibleHttp11ConnectionHandler.class);
        protected Http11Protocol proto;
        protected static int count=0;

        protected LegacyAccessibleHttp11ConnectionHandler(Http11Protocol proto) {
            super(proto);
            this.proto = proto;
        }

        public void setAttribute( String name, Object value ) {
        }

        public void setServer( Object o ) {
        }

        public Object[] init() {
            return afterInit(super.init());
        }
        
        protected PoolTcpEndpoint getEndpoint() {
            return proto.ep;
        }
        
        protected ThreadPool getThreadPool() {
            return proto.tp;
        }
        
        protected Object[] afterInit(Object[] thData) {
            // was set up by super
            Http11Processor  processor = (Http11Processor)
                    thData[ Http11BaseProtocol.THREAD_DATA_PROCESSOR];
            
            thData[Http11BaseProtocol.THREAD_DATA_PROCESSOR]=processor;

            if( proto.getDomain() != null ) {
                try {
                    RequestInfo rp=processor.getRequest().getRequestProcessor();
                    rp.setGlobalProcessor(global);
                    ObjectName rpName=new ObjectName
                        (proto.getDomain() + ":type=RequestProcessor,worker="
                         + proto.getName() +",name=HttpRequest" + count++ );
                    Registry.getRegistry(null, null).registerComponent( rp, rpName, null);
                    thData[Http11BaseProtocol.THREAD_DATA_OBJECT_NAME]=rpName;
                } catch( Exception ex ) {
                    log.warn("Error registering request");
                }
            }
            return thData;
        }

    }
}
